package exp.ranking;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.TreeMap;

import opennlp.tools.sentdetect.SentenceDetectorME;
import tools.ObjectTools;
import tools.nlp.SentenceDetector;
import tools.twitter.TweetTidy;
import dragon.ir.summarize.ROUGE;
import exp.URLContent;
import exp.URLContent1;
import exp.web.cnn.CNNExtraction;
import exp.web.usatoday.USATodayExtraction;
/**
 * Evaluate the sentences according to the highlights using ROUGE score
 * @author Lanjun
 *
 */
public class Ex2 {
	
	static int outCount = 5;
	
	public void run(String hPath, String hcPath, int N) {
		SentenceDetector sd = new SentenceDetector();
		SentenceDetectorME sdm = sd.getSentenceDetector();
		
		for (File file : (new File(hPath)).listFiles()) {
//			ArrayList<String> hll = new ArrayList<>();
			
			@SuppressWarnings("unchecked")
			HashMap<String, URLContent> hls = (HashMap<String, URLContent>) ObjectTools.readFromFile(file);
			
			ArrayList<URLContent1> hls1 = new ArrayList<>();
			
			// url to URLContent
			HashMap<String, URLContent> url2Content = new HashMap<>();

			for (URLContent temp : hls.values()) {
				URL tempURL = null;
				try {
					tempURL = new URL(temp.url);
				} catch (MalformedURLException e) {
					e.printStackTrace();
				}
				String host = tempURL.getHost();
				if (temp.highlights != null) {
					if (host.equals("edition.cnn.com") && (!temp.url.startsWith("http://edition.cnn.com/video"))
							&& (!temp.url.startsWith("http://edition.cnn.com/SPECIALS"))
							&& (!temp.url.startsWith("http://edition.cnn.com/interactive"))) {
						url2Content.put(CNNExtraction.unify(temp.url), CNNExtraction.tidy(temp));
					} else if (host.equals("www.usatoday.com")
							&& (!temp.url.startsWith("http://www.usatoday.com/videos"))) {
						url2Content.put(USATodayExtraction.unify(temp.url), USATodayExtraction.tidy(temp));
					}
				}
			}
			ROUGE r = new ROUGE();
			r.useRougeN(N);
			r.setMultipleReferenceMode(1);
			for (String url : url2Content.keySet()) {
				URLContent uc = url2Content.get(url);
				ArrayList<TreeMap<Double, String>> sortedList = new ArrayList<>();
				
				//initialize uc1
				URLContent1 uc1 = new URLContent1();
				uc1.title = uc.title;
				uc1.contents = uc.contents;
				uc1.highlights = uc.highlights;
				uc1.shortURL = uc.shortURL;
				uc1.url = uc.url;
				uc1.tweets = uc.tweets;
				
				System.out.println(file.getName());
				System.out.println(uc.url);
				System.out.println(uc.title);
//				hll.addAll(uc.highlights);
				//Calculate the ROUGE score for each sentence to the highlights
				for (String h : uc.highlights) {
					h = TweetTidy.doTidyHTML(h);
					TreeMap<Double, String> sorted = new TreeMap<>();
					String [] hl = new String[1];
					hl[0] = h;
					for (String c : uc.contents) {
						c = TweetTidy.doTidyHTML(c);
						String [] sens = sdm.sentDetect(c);
						for (String sen : sens) {
							r.evaluate(sen, hl);
//							System.out.println((sen + "<" + h + ">"));
//							r.printResult();
							Double f = r.getFScore();
							if (sorted.containsKey(f)) {
								sorted.put(f, sorted.get(f) + "\t,,\t" + sen);
							} else {
								sorted.put(f, sen);
							}
						}
					}
					sortedList.add(sorted);
					System.out.println("=================================================");
					System.out.println(h);
					System.out.println("=================================================");
					int count = 0;
					for (Double f : sorted.descendingKeySet()) 
					{
						if (f == 0.0) {
							break;
						}
						System.out.print(f + "\t");
						System.out.println(sorted.get(f));
						count++;
						if (count == outCount) {
							break;
						}
					}
				}
				System.out.println("=================================================\n");
				uc1.sortedContent = sortedList;
				hls1.add(uc1);
			}
			ObjectTools.writeToFile(hls1, hcPath + file.getName());
		}
	}
	
	public static void main(String[] args) {
		String hPath = "data/objs/highlights/";
		String oPath = "data/objs/highlights_contents_ROUGE/";
		int N = 2;
		
		Ex2 exp2 = new Ex2();
		exp2.run(hPath, oPath, N);
	}
}
